﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Binarysharp.MemoryManagement;
using System.Diagnostics;
using System.Threading;

namespace RetroGlider
{
    class ItemSpellInteraction
    {
        static public MemorySharp sharp;

        int pid = 0;

        public ItemSpellInteraction()
        {
            foreach (Process process in Process.GetProcessesByName("Wow"))
            {
                pid = process.Id;
            }
            sharp = new MemorySharp(Process.GetProcessById(pid));
        }

        public void CastSpell(string spell_name, int rank)
        {
            SendLuaCommand("CastSpellByName(\"" + spell_name + "(rank "+rank+")\")");

        }

        public void SendEmote(string emote)
        {
            SendLuaCommand("DoEmote("+emote+")");
        }

        private void SendLuaCommand(string Command)
        {

            

            byte[] tempBytes = new byte[0];

            try
            {

                sharp.Threads.MainThread.Suspend();

                //Allocate Memory For Command

                var DoStringArg_Codecave = sharp.Memory.Allocate(Encoding.UTF8.GetBytes(Command).Length + 1);

                //Execute Address
                IntPtr FrameScript_Execute = new IntPtr(0x00704CD0);

                //Write Command In Allocated Memory
                sharp.WriteString(DoStringArg_Codecave.BaseAddress, Command, false);

                var asm = new[] 
		        {
			        "mov ecx, " + DoStringArg_Codecave.BaseAddress,
			        "mov edx, " + DoStringArg_Codecave.BaseAddress,
			        "call " + FrameScript_Execute,
			        "retn",    
		        };

                //Inject and Execute
                sharp.Assembly.InjectAndExecute(asm);
                //Free Memory Allocated For Command
                sharp.Memory.Deallocate(DoStringArg_Codecave);

                sharp.Threads.MainThread.Resume();

                Thread.Sleep(1);
            }
            catch { }            

            //return tempBytes;

        }

        
        public string GetLocalizedText(string Command)
        {

            uint FrameScript__GetLocalizedText = 0x703BF0;

            uint ClntObjMgrGetActivePlayerObj = 0x00468550;

            var DoStringArg_Codecave = sharp.Memory.Allocate(Encoding.UTF8.GetBytes(Command).Length + 1);

            //This is broken WTF is going on here.
            //sharp.Write(DoStringArg_Codecave, Encoding.UTF8.GetBytes(Command),10);            
            sharp.Write<byte>(DoStringArg_Codecave.BaseAddress, Encoding.UTF8.GetBytes(Command));

            //var byteArray = sharp.Read<byte>(address, 10);

            String[] asm = new String[]
            {
                "call " + ClntObjMgrGetActivePlayerObj,                            
                "mov ecx, eax",
                "push -1",
                "mov edx, " + DoStringArg_Codecave.BaseAddress + "",
                "push edx",
                "call " + FrameScript__GetLocalizedText,
                "retn",
            };

            IntPtr inject = sharp.Assembly.InjectAndExecute(asm);

            //string sResult = Encoding.ASCII.GetString(InjectAndExecute(asm));
            string sResult = sharp.ReadString(inject);

            sharp.Memory.Deallocate(DoStringArg_Codecave);

            return sResult;
        }
         


    }
}
